package com.archie.zhufengfm;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.archie.zhufengfm.entity.Track;
import com.archie.zhufengfm.service.MusicPlayService;

import java.util.ArrayList;

public class PlayActivity extends Activity implements View.OnClickListener {

    ImageView play_pre, play_play, play_next;

    private ArrayList<Track> tracks;

    // 当前播放位置
    private int index;
    private Intent playServiceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        initView();

        initMusicPlayService();
    }

    // 初始化音乐播放服务
    private void initMusicPlayService() {

        tracks = getIntent().getParcelableArrayListExtra("tracks");
        index = getIntent().getIntExtra("tracks_index", 0);

        // 开启服务
        playServiceIntent = new Intent(this, MusicPlayService.class);
        playServiceIntent.putExtra(Constants.SERVICE_TRACK_PLAY_URL, tracks.get(index).getPlayUrl64());

        startService(playServiceIntent);
    }

    // 初始化控件
    private void initView() {

        play_next = (ImageView) findViewById(R.id.activity_play_next);
        play_next.setOnClickListener(this);

        play_play = (ImageView) findViewById(R.id.activity_play_play);
        play_play.setOnClickListener(this);

        play_pre = (ImageView) findViewById(R.id.activity_play_pre);
        play_pre.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        switch (id){

            case R.id.activity_play_pre:

                if(index == 0) {

                    Toast.makeText(this, "已是第一首", Toast.LENGTH_SHORT).show();

                }else{

                    index --;

                    // 更新服务
                    playServiceIntent.putExtra(Constants.SERVICE_TRACK_PLAY_URL, tracks.get(index).getPlayUrl64());

                    startService(playServiceIntent);

                }

                break;

            case R.id.activity_play_play:


                break;

            case R.id.activity_play_next:

                if(index == tracks.size() - 1) {

                    Toast.makeText(this, "已至末尾", Toast.LENGTH_SHORT).show();

                }else{

                    index ++;

                    // 更新服务
                    playServiceIntent.putExtra(Constants.SERVICE_TRACK_PLAY_URL, tracks.get(index).getPlayUrl64());

                    startService(playServiceIntent);

                }

                break;
        }
    }
}
